package com.imstlife.vending.activity;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.imstlife.vending.R;
import com.imstlife.vending.utils.AnimatorUtil;
import com.imstlife.vending.utils.BackgroundVideo;
import com.imstlife.vending.utils.Cof;
import com.imstlife.vending.utils.GlideUtil;
import com.imstlife.vending.utils.QrCodeUtil;
import com.imstlife.vending.utils.SharePersistentUtil;
import com.imstlife.vending.vo.DrinkInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lihaifeng on 16/10/31.
 */
public class DrinkInfoActivity extends BaseActivity implements SurfaceHolder.Callback, View.OnClickListener {


    private static final String TAG = "DrinkInfoActivity";
    /**
     * 接收推送广播
     */
    private MainAcNotificationReceiver receiver;
    /**
     * 当前选中的饮品
     */
    private DrinkInfo drinkInfo;

    private VideoView bgVideoView;

    /**
     * 商品图
     */
    private ImageView ivDrink;

    /**
     * 商品名
     */
    private TextView tvDrinkName;

    /**
     * 商品信息
     */
    private TextView tvDrinkInfo;

    /**
     * 商品价格
     */
    private TextView tvPrice;

    /**
     * 返回按钮
     */
    private Button btnBack;

    /**
     * 购买按钮
     */
    private Button btnBuy;

    /**
     * 二维码
     */
    private ImageView ivQr;

    /**
     * 文字提示
     */
    private TextView tvTishi;

    private SimpleDateFormat sdf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drink_info);
        drinkInfo = (DrinkInfo) getIntent().getSerializableExtra("drinkinfo");

        initView();

    }

    /**
     * 上下抖动动画
     */
    ValueAnimator animator;

    private void initView() {
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        bgVideoView = (VideoView) findViewById(R.id.vv_ac_dinrk_info_bg);
        //加载背景视频
        BackgroundVideo.initBackGroundVideo(this, getWindowManager(), bgVideoView);

        ivDrink = (ImageView) findViewById(R.id.iv_ac_main_drink_info_drink);
        GlideUtil.dontAnimLoad(this, drinkInfo.getMinPicUrl(), ivDrink);
        animator = AnimatorUtil.animalDrink(ivDrink);

        tvDrinkInfo = (TextView) findViewById(R.id.text_ac_main_drink_info);
        tvDrinkInfo.setText(drinkInfo.getText());

        tvDrinkName = (TextView) findViewById(R.id.text_ac_main_drink_name);
        tvDrinkName.setText(drinkInfo.getName());

        tvPrice = (TextView) findViewById(R.id.tv_ac_drink_info_price);
        tvPrice.setText("￥ " + drinkInfo.getPrice() + "");

        btnBack = (Button) findViewById(R.id.btn_ac_drink_info_back);
        btnBack.setOnClickListener(this);
        btnBuy = (Button) findViewById(R.id.btn_ac_drink_info_buy);
        btnBuy.setOnClickListener(this);

        ivQr = (ImageView) findViewById(R.id.iv_ac_drink_info_qr);
        tvTishi = (TextView) findViewById(R.id.tv_ac_drink_info_msg);


    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Cof.What.OUTING_PLS_WAIT:
                    ivQr.setVisibility(View.GONE);
                    ivDrink.setVisibility(View.VISIBLE);
                    tvTishi.setText("正在出货，请等待....");
                    break;
            }
        }
    };


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ac_drink_info_back:
                finish();
                break;
            case R.id.btn_ac_drink_info_buy:
//                 out(Integer.valueOf(drinkInfo.getId()),"");
                showDrinkQr();
//                out(1,"");
                break;
        }
    }

    @Override
    public void autoReturn() {
        super.autoReturn();
        finish();
    }

    /**
     * 根据notifyUrl动态配置二维码内容跳转到不同公司的web支付页面，?之后作为get参数  必传
     */
    private void showDrinkQr() {
        String qrStr = notifyUrl +"?"+
                "drinkName=" + drinkInfo.getName() +
                "&drinkPrice=" + drinkInfo.getPrice() +
                "&time=" + sdf.format(new Date()) +
                "&deviceId=" +SharePersistentUtil.getSharePersistentString(DrinkInfoActivity.this,Cof.KEY_DEVICE_ID) +

                "&drinkImgUrl=" + drinkInfo.getMinPicUrl() +
                "&id=" + drinkInfo.getId()+
                "&num=" + drinkInfo.getNum();
        Log.i(TAG,"qrStr:"+qrStr);

        ivDrink.setVisibility(View.INVISIBLE);
        ivQr.setImageBitmap(QrCodeUtil.generateQrCode(qrStr, 200));
        tvTishi.setText("扫一扫");
    }

    /**
     * 发起出货串口指令后的结果   state 0  成功
     * @param state
     */
    @Override
    public void outResult(int state) {
        super.outResult(state);
        Log.i(TAG,"outResult"+state);
        finish();
    }

    /**
     * 推送中断
     */
    @Override
    public void disconnect() {
        super.disconnect();
        Message msg = Message.obtain();
        msg.what = Cof.What.PUSH_DISCONNECTED;
        mHandler.sendMessage(msg);
    }

    /**
     * 重写父类  正在出货 请等待
     */
    @Override
    public void plsWait() {
        super.plsWait();
        Message msg = Message.obtain();
        msg.what = Cof.What.OUTING_PLS_WAIT;
        mHandler.sendMessage(msg);
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.i(TAG, "onPause");
        super.onPause();
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestory...");
    }

}
